<?php
/**
* Mestengo Application Framework Copyright (c) 2010-2011 Michael De Soto. This 
* program is free software: you can redistribute it and/or modify it under the 
* terms of the GNU General Public License as published by the Free Software 
* Foundation, either version 3 of the License, or any later version.
*
* @author Michael De Soto <michael@de-soto.net>
* @copyright Copyright (c) 2011, Michael De Soto 
* @package Mestengo
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License
* @version $Id$
*/

class CGI
{
    //private static $__COOKIE = array();
    private static $__GET = array();
    private static $__PARAMETERS = array();
    private static $__POST = array();

    final public function __construct()
    {
        // Initialize query parameters.
        if (isset($_GET))
        {
            self::$__GET = $_GET;
        }

        if (isset($_POST))
        {
            self::$__POST = $_POST;
        }
        
        // TODO: Handle Cookies. 
        //if (isset($_COOKIE))
        //{
        //    self::$__COOKIE = $_COOKIE;
        //}
        
        // Combine GET and POST parameters and then normalize keys.
        self::$__PARAMETERS = array_merge(self::$__GET, self::$__POST);
        self::$__PARAMETERS = array_change_key_case(self::$__PARAMETERS, CASE_LOWER);
    }

    
    public function header()
    {
        return "Content type: text/html\n\n";
    }

    
    /**
     * The param() method returns the value of the suspect parameter. If suspect
     * is not provided, the method returns an array of all known parameters. If
     * a value is also passed, it is used to set the value of suspect.
     * 
     * @access public
     * @param string $suspect
     * @param string|array $value
     * @return mixed
     */
    final public function param($suspect = null, $value = null)
    {
        if (!is_null($suspect))
        {
            // Normalize key.
            $suspect = strtolower($suspect);
            
            // Set parameter if method called with value.
            if (!is_null($value))
            {
                return  self::$__PARAMETERS[$suspect] = $value;
            }
            
            // Otherwise return the value stored in suspect.
            if (isset(self::$__PARAMETERS[$suspect]))
            {
                return self::$__PARAMETERS[$suspect];
            }
            
            // Or null if suspect unassigned.
            return null;
        }

        return self::$__PARAMETERS;
    }
}